// pages/TopicsModule/singleTopic.js
const app = getApp()
Page({
    data: {
        statusBarHeight: app.globalData.statusBarHeight,//手机状态栏高度
        barId: null,
        barInfo: {
            barId: null,//该版面的 id
            barName: null,//该版面的名称
            introduction: null,//该版面的简介
            avatarUrl: null,//该版面的头像地址
            barType: null,//版面类型(1`-普通，2-热门，3-置顶）
            followerSum: null,//关注该吧的人数
            postSum: null,//该版面中的贴文总数
            popularity: null,//该版面的热度
            barPrincipal: null,//该版版主的userId列表
            barAdmin: null,//改版的管理员列表
            createTime: null,//改版的创建时间
            creatorId: null,//创建者id
            hasFollow: null//是否已关注该版面
        },
        currentPage: 1,
        pageSize: 10,//每一页的帖子数量
        postList: {},
        /*
        postId      帖文id
        barId       该贴文所在的版面id
        postType    该贴文的类型，0-正常，1-热门，2-加精，3-置顶
        postTitle   贴文标题
        postText    贴文内容，html格式
        creatorId   发帖人id
        createTime  发帖时间（yyyy-MM-dd HH:mm:ss）
        popularity  贴文热度
        lastEditTime 贴文最后一次修改时间（yyyy-MM-dd HH:mm:ss）
        viewSum     该贴文的浏览次数
        likeSum     点赞次数
        collectSum  该贴文的收藏次数
        dislikeSum  该贴文的踩次数
        commentSum  该贴文的评论数
        reportSum   该贴文被举报的次数
        hasLike     是否点赞
        hasDislike  是否踩
        hasCollect  是否收藏
        creatorName     >发帖人昵称
        creatorAvatar   >发帖人头像
        */
        scrollTop: 0,
        nowScrollTop: 0,
        toTopDisplay: 'none'
    },

    onLoad: function (options) {
        var that = this
        console.log(options.barid)
        that.setData({
            barId: options.barid
        })
        that.initBarInfo()
        that.initPostList()
    },
    onShow: function(){
        var that = this
        that.initPostList()
    },
    initBarInfo(){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}//bar/getBarConfig`,
            data: {
                barId: that.data.barId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                that.setData({
                    barInfo: res.data.result
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    initPostList(){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}//bar/getPostList`,
            data: {
                barId: that.data.barId,
                currentPage: that.data.currentPage,
                pageSize: that.data.pageSize
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                var postList = res.data.result.postList
                that.setData({
                    postList: postList
                })
                for(var i=0; i<postList.length; i++){
                    that.getUserDetail(i)
                }
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    getUserDetail(i){
        var that = this
        var userId = that.data.postList[i].creatorId
        wx.request({
            url: `http://${app.globalData.target}//user/displayedInfo`,
            data: {
                userId: userId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                that.setData({
                    [`postList[${i}].creatorName`]: res.data.result.userName,
                    [`postList[${i}].creatorAvatar`]: res.data.result.avatarPath
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },

    gotoPost: function(e){
        var postid = e.currentTarget.dataset.postid
        //跳转到相应的帖子页面
        wx.navigateTo({
            url: `../PostsModule/generalPost?postid=${postid}`
        })
    },
    follow: function(e){
        var that = this
        var followerSum = that.data.barInfo.followerSum
        wx.request({
            url: `http://${app.globalData.target}//bar/follow`,
            data: {
                barId: that.data.barId
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                if(res.data.code == 0){
                    if(that.data.barInfo.hasFollow == true){
                        that.setData({
                            'barInfo.hasFollow': false,
                            'barInfo.followerSum': followerSum - 1
                        })
                    }else{
                        that.setData({
                            'barInfo.hasFollow': true,
                            'barInfo.followerSum': followerSum + 1
                        })
                    }
                }
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    scrollBinder: function(e){
        var that = this
        var top = e.detail.scrollTop
        that.setData({
            nowScrollTop: top
        })
        if(that.data.toTopDisplay == 'none'){
            if(top > 400){
                that.setData({
                    toTopDisplay: 'block'
                })
            }
        }else{
            if(top < 400){
                that.setData({
                    toTopDisplay: 'none'
                })
            }
        }
    },
    addPost: function(e){
        var that = this
        var barid = that.data.barId
        wx.navigateTo({
            url: `../TopicsModule/addPost?barid=${barid}`
        })
    },
    returnToTop: function(e){
        var that = this
        console.log(e)
        that.setData({
            scrollTop: 0
        })
    },

    handleToLower: function(){
        console.log("触发上拉加载更多")
        var that = this
        wx.showLoading({
            title: '正在加载',
        })
        //测试用，模拟“加载更多”接口返回数据，实现中要判断是否还有更多
        var postList = that.data.postList
        var newScrollTop = that.data.nowScrollTop + 40
        var newPage = that.data.currentPage + 1
        //请求下一页列表数据
        wx.request({
            url: `http://${app.globalData.target}//bar/getPostList`,
            data: {
                barId: that.data.barId,
                currentPage: newPage,
                pageSize: that.data.pageSize
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                var nextList = res.data.result.postList
                //判断是否为空
                console.log(nextList.length)
                if(nextList.length != 0){
                    that.setData({
                        postList: postList.concat(nextList),
                        scrollTop: newScrollTop,
                        currentPage: newPage
                    })
                    var _postList = that.data.postList
                    for(var i=0; i<_postList.length; i++){
                        that.getUserDetail(i)
                    }
                }
            },
            fail: function(e) {
                console.log('fail to get postList')
                console.log(e)
            }
        })
        wx.hideLoading()
    }

})